# -*-coding:utf-8-*-

# http://www.nohup.cc/article/110/
# 要求python > 3.5.0
from typing import Union, List
import numpy as np


def CKSAAP(sequence: str,
           k: int,
           order: Union[str, None] = None) -> List:
    '''
    计算Compositon of k-spaced Amino Acid Pairs
    :param sequence: string  序列
    :param k: int  间隔
    :param order: Union[str,None] 默认"ACDEFGHIKLMNPQRSTVWY-"
    :return: List 21*21=441维度
    '''
    if order == None:
        order = "ACDEFGHIKLMNPQRSTVWY-"

    aaPairsSum = {}

    for i in order:
        for j in order:
            aaPairsSum[i + j] = 0

    sum = len(sequence) - k - 1

    for i in range(sum):
        aaPairsSum[sequence[i] + sequence[i + k + 1]] = aaPairsSum[sequence[i] + sequence[i + k + 1]] + 1
        # print(sequence[i]+sequence[i+k+1])
    encodings = []
    for value in aaPairsSum.values():
        encodings.append(int(value) / sum * 1.00)

    return encodings


def one_CKSAAP(data, windows=15):
    data = data
    length = len(data)
    klist = [0, 1, 2, 3]
    data_X = np.zeros((length, len(klist), 441))
    data_Y = []
    for i in range(length):
        x = data[i].split(',')
        data_Y.append(int(x[4]))
        for j in range(len(klist)):
            aaindex = np.array(CKSAAP(x[5],j))
            data_X[i][j] = aaindex
    data_Y = np.array(data_Y)
    return data_X, data_Y


if __name__ == '__main__':
    f_r_train = open('hit-40/totalsum.csv')
    train_data = f_r_train.readlines()
    result = one_CKSAAP(train_data)
    print(result)
